//
//  ComposeShareView.m
//  SinaWebo
//
//  Created by wwj on 16/1/11.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "ComposeShareView.h"
#import "ComposeBtn.h"
#import <POP.h>

@interface ComposeShareView ()

@end

@implementation ComposeShareView

+ (instancetype)composeShareView{
    return [[self alloc] init];
}

-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
}

-(void)setImgArray:(NSArray *)imgArray{
    _imgArray = imgArray;
    [self reloadView];
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)initView{
    
}

-(void)reloadView{
    CGFloat bW = kScreenW / 3;
    CGFloat bH = bW;
    for (int index = 0; index< _titleArray.count; index++) {
        int col = index / 3;
        int row = index % 3;
        CGFloat bX = row * bW;
        CGFloat bY = col * bH;
        ComposeBtn *btn = [ComposeBtn composeBtn];
        [btn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBtn:)]];
        btn.title = _titleArray[index];
        btn.img = _imgArray[index];
        btn.tag = index;
        
        POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        animation.fromValue = [NSValue valueWithCGRect:CGRectMake(bX, kScreenH, bW, bH)];
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(bX, bY, bW, bH)];
        animation.beginTime = CACurrentMediaTime() + 0.05 * index;
        animation.springSpeed = 10.0;
        animation.springBounciness = 10.0;
        [btn pop_addAnimation:animation forKey:nil];
        [self addSubview:btn];
        
    }
    
}

-(void)onClickBtn:(UITapGestureRecognizer *)tapGesture{
    ComposeBtn *btn = (ComposeBtn *)tapGesture.view;
    [btn onClickBtn];
    [self changeBig:btn];
    if ([_delegate respondsToSelector:@selector(composeShareView:clickBtnType:)]) {
        [_delegate composeShareView:self clickBtnType:(int)btn.tag];
    }
}

-(void)changeBig:(ComposeBtn *)btn{
    for (ComposeBtn *model in self.subviews) {
        if (btn.tag != model.tag) {
            [model changeSize];
        }
    }
}

-(void)hidden:(void (^)())finishBlock{
    NSInteger count = self.subviews.count - 1;
    for (NSInteger index = count; index >=0; index--) {
        ComposeBtn *btn = self.subviews[index];
        POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(btn.x, kScreenH, btn.width, btn.height)];
        animation.beginTime = CACurrentMediaTime() + 0.05 * index;
        
        if (index == 3) {
            [animation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                if (finishBlock) {
                    finishBlock();
                }
            }];
        }
        
        [btn pop_addAnimation:animation forKey:nil];
    }
}

-(void)setFrame:(CGRect)frame{
    frame.size.height = 250;
    [super setFrame:frame];
}

@end
